package com.huilian.iotbox.admin.service.impl;

import com.huilian.iotbox.admin.dao.BrandInfoDao;
import com.huilian.iotbox.admin.dao.CustomerServiceInfoDao;
import com.huilian.iotbox.admin.service.BrandInfoService;
import com.huilian.iotbox.admin.service.CustomerServiceInfoService;
import com.huilian.iotbox.admin.service.MerchantStoreService;
import com.huilian.iotbox.admin.service.UserService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.CustomerServiceInfoDto;
import com.huilian.iotbox.data.po.BrandInfo;
import com.huilian.iotbox.data.po.CustomerServiceInfo;
import com.huilian.iotbox.data.po.MerchantStore;
import com.huilian.iotbox.data.po.User;
import com.huilian.iotbox.data.vo.BrandInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @author chenqiwen
 * @CreateDate: 2022-04-28-14-04
 * @Description:
 */
@Service
public class CustomerServiceInfoServiceImpl implements CustomerServiceInfoService {
    @Autowired
    private CustomerServiceInfoDao customerServiceInfoDao;
    @Resource
    private BrandInfoDao brandInfoDao;
    @Resource
    private BrandInfoService brandInfoService;
    @Resource
    private MerchantStoreService merchantStoreService;
    @Resource
    private UserService userService;

    @Override
    public CustomerServiceInfo findOneByMerchantStoreId(Integer merchantStoreId) {
        return customerServiceInfoDao.findOneByMerchantStoreId(merchantStoreId);
    }

    @Override
    public CustomerServiceInfo findCustomerServiceInfoByStoreId(CustomerServiceInfoDto customerServiceInfoDto) {
        CustomerServiceInfo customerServiceInfo = new CustomerServiceInfo();
        CustomerServiceInfo serviceInfo = customerServiceInfoDao.findOneByMerchantStoreId(customerServiceInfoDto.getMerchantStoreId());
        if (!StringUtils.isEmpty(serviceInfo)) {
            BeanUtils.copyProperties(serviceInfo, customerServiceInfo);
        } else {
            BrandInfoVo brandInfoVo = brandInfoDao.findOneBrandInfoByUserId(customerServiceInfoDto.getUserId());
            if (!StringUtils.isEmpty(brandInfoVo)) {
                customerServiceInfo.setServiceWeChatQr(!StringUtils.isEmpty(brandInfoVo.getServiceWeChatQr()) ? brandInfoVo.getServiceWeChatQr() : "");
                customerServiceInfo.setServiceWeChatNumber(!StringUtils.isEmpty(brandInfoVo.getServiceWeChatNumber()) ? brandInfoVo.getServiceWeChatNumber() : "");
                customerServiceInfo.setServiceMobilePhone(!StringUtils.isEmpty(brandInfoVo.getServiceMobilePhone()) ? brandInfoVo.getServiceMobilePhone() : "");
                customerServiceInfo.setServiceFixedTelephone(!StringUtils.isEmpty(brandInfoVo.getServiceFixedTelephone()) ? brandInfoVo.getServiceFixedTelephone() : "");
            }
        }
        return customerServiceInfo;
    }

    /**
     * 获取客服电话
     *
     * @param merchantStoreId
     * @return
     */
    @Override
    public CustomerServiceInfo getConsumerInfo(Integer merchantStoreId) {
        CustomerServiceInfo customerServiceInfo = customerServiceInfoDao.findOneByMerchantStoreId(merchantStoreId);
        if (ObjectUtils.isEmpty(customerServiceInfo) || ObjectUtils.isEmpty(customerServiceInfo.getServiceMobilePhone())) {
            BrandInfo brandInfo;
            MerchantStore merchantStore = null;
            customerServiceInfo = new CustomerServiceInfo();

            if (ObjectUtils.isEmpty(customerServiceInfo.getBrandInfoId())) {
                brandInfo = brandInfoService.findById(customerServiceInfo.getBrandInfoId());
            } else {
                merchantStore = merchantStoreService.findMerchantStoreById(merchantStoreId);
                brandInfo = brandInfoService.findByUserId(merchantStore.getUserId());
            }
            if (ObjectUtils.isEmpty(brandInfo)) {
                if (ObjectUtils.isEmpty(merchantStore)) {
                    merchantStore = merchantStoreService.findMerchantStoreById(merchantStoreId);
                }
                User user = userService.findUserById(merchantStore.getUserId());
                if (!ObjectUtils.isEmpty(user.getUsername())) {
                    customerServiceInfo.setServiceMobilePhone(user.getUsername());
                }
            } else {
                customerServiceInfo.setServiceMobilePhone(brandInfo.getServiceMobilePhone());
            }
            return customerServiceInfo;
        } else {
            return customerServiceInfo;
        }

    }

    @Override
    public ServerResponse editCustomerServiceInfo(CustomerServiceInfoDto customerServiceInfoDto) {
        CustomerServiceInfo serviceInfo = customerServiceInfoDao.findOneByMerchantStoreId(customerServiceInfoDto.getMerchantStoreId());
        int result = 0;
        if (StringUtils.isEmpty(serviceInfo)) {
            BrandInfoVo brandInfoVo = brandInfoDao.findOneBrandInfoByUserId(customerServiceInfoDto.getUserId());
            if(StringUtils.isEmpty(brandInfoVo)){
                result = customerServiceInfoDao.insert(customerServiceInfoDto);
            }else{
                result = brandInfoDao.update(customerServiceInfoDto);
            }
        } else{
            result = customerServiceInfoDao.update(customerServiceInfoDto);
        }
        if(result <= 0){
            throw new RuntimeException("修改失败");
        }
        return ServerResponse.createBySuccess();
    }

}